<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/12-16:04
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\controller;

    use app\wechat\queue\WechatQueue;
    use app\wechat\service\WechatService;
    use think\Db;
    use think\exception\HttpResponseException;


    class Fans extends ChatBase
    {

        /**
         * 绑定数据表
         * @var string
         */
        protected $table = 'WechatFans';


        /**
         * @throws \think\Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/13
         * @name: index
         * @describe:
         */
        public function index()
        {

           if ($this->request->isAjax()){
               $param       = $this->request->param();
               $where=[];
               if (isset($param['nickname']) && $param['nickname']!=""){
                  $where[]=['nickname','like', "%".$param['nickname']."%"];
               }

               if (isset($param['subscribe']) && $param['subscribe']!='2'){
                   $where[]=['subscribe','eq', $param['subscribe']];
               }

               if (isset($param['is_black']) && $param['is_black']!='2'){
                   $where[]=['is_black','eq', $param['is_black']];
               }

               if (isset($param['subscribe_at']) && !empty($param['subscribe_at'])){
                   $ar=explode("|",$param['subscribe_at']);
                   $where[]=['subscribe_at','between time', ["$ar[0]","$ar[1]"]];
               }

               $where[]=['appid',"eq",WechatService::getAppid()];

               $data=Db::name($this->table)->where($where)->order("subscribe_time desc")->page($param['page'])->limit($param['limit'])->select();


               $count=Db::name($this->table)->where($where)->count("id");

               return $this->ajaxResult($data,0,'',$count);
           }
           return $this->fetch();

        }



        /**
         * @param array $data
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: _index_page_filter
         * @describe: 列表数据处理
         */
        protected function _index_page_filter(array &$data)
        {
            $tags = Db::name('WechatFansTags')->column('id,name');
            foreach ($data as &$user) {
                $user['tags'] = [];
                foreach (explode(',', $user['tagid_list']) as $tagid) {
                    if (isset($tags[$tagid])) {
                        $user['tags'][] = $tags[$tagid];
                    }
                }
            }
        }

        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: setBlack
         * @describe:批量拉黑粉丝
         */
        public function setBlack()
        {
            if ($this->request->isPost()){
                try {
                    $opendi=$this->request->post('ids');

                   // dump($opendi);
                    foreach (array_chunk(explode(',', $opendi), 20) as $openids) {

                       WechatService::WeChatUser()->batchBlackList($openids);

                        Db::name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '1']);
                    }

                    $this->okSend('拉黑粉丝信息成功！');

                } catch (HttpResponseException $exception) {

                    throw  $exception;
                } catch (\Exception $e) {

                    $this->okSend("拉黑粉丝信息失败，请稍候再试！{$e->getMessage()}",0);
                }
            }

        }

        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: delBlack
         * @describe:取消拉黑粉丝
         */
        public function delBlack()
        {
            if ($this->request->isPost()){
                try {
                    $opendi=$this->request->post('ids');

                    foreach (array_chunk(explode(',', $opendi), 20) as $openids) {
                        WechatService::WeChatUser()->batchUnblackList($openids);

                        Db::name('WechatFans')->whereIn('openid', $openids)->update(['is_black' => '0']);
                    }

                    $this->okSend('取消拉黑粉丝信息成功！');

                } catch (HttpResponseException $exception) {
                    throw  $exception;

                } catch (\Exception $e) {
                    $this->okSend("取消拉黑粉丝信息失败，请稍候再试！{$e->getMessage()}",0);
                }
            }
        }

        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: sync
         * @describe:同步粉丝列表
         */
        public function sync()
        {
            if ($this->request->isPost()){
                try {
                    $this->appid = WechatService::getAppid();

                    addLog("微信管理","创建微信[{$this->appid}]粉丝同步任务");

                    sysQueue("同步[{$this->appid}]粉丝列表", WechatQueue::URI, 0, ['appid' => $this->appid], 0);

                    $this->okSend('创建同步粉丝任务成功，需要时间来完成。<br>请到 系统管理 > 任务管理 查看执行进度！');

                } catch (HttpResponseException $exception) {
                    throw $exception;
                } catch (\Exception $e) {
                    $this->okSend("创建同步粉丝任务失败，请稍候再试！<br> {$e->getMessage()}",0);
                }
            }
        }

        /**
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12
         * @name: remove
         * @describe:删除粉丝信息
         */
        public function remove()
        {
            $this->_delete($this->table);
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/13
         * @name: del
         * @describe:删除
         */
        public function del(){
            if ($this->request->isPost()){
                try {
                    $opendi=$this->request->post('ids');

                    foreach (array_chunk(explode(',', $opendi), 20) as $openids) {

                        Db::name('WechatFans')->whereIn('id', $openids)->delete();
                    }
                    $this->okSend('删除粉丝信息成功！');

                } catch (HttpResponseException $exception) {
                    throw  $exception;

                } catch (\Exception $e) {
                    $this->okSend("删除粉丝信息失败，请稍候再试！{$e->getMessage()}",0);
                }
            }
        }


    }